0.2
switch
StatementsHere is what a switch statement looks like:
switch ( integerExpression ) { case label1 : statementList1 break; case label2 : statementList2 break; case label3 : statementList3 break; . . . other cases like the above default: defaultStatementList }
Here is how it works:
switch
statement.integerExpression
determines which case is selected.integerExpression
must evaluate to an integer
type
(including char
).label
must be an integer literal (like 0, 23, or 'A'), but not
an expression or variable.statementList
.statementList
is usually followed with break;
switch
statement is executed, the following happens:
integerExpression
is evaluated.label
s after each case
are inspected one by one,
starting with the first.statementList
execute.break
statement is encountered.switch
statement is complete.integerExpression
, then
the default
case is picked, and its statements execute.Would you believe that there are even more rules for the break-statement?